import collections
class Solution(object):
    def minChanges(self, nums, k):
        c = []
        d = []
        n = len(nums)
        for i in range(k):
            c.append(collections.Counter(nums[i::k]))
            d.append((n + k - 1 - i)// k)
        m = len(c)
        max_range = 2 ** 10
        st = [d[0]] * max_range
        for key in c[0]:
            st[key] = d[0] - c[0][key]
        for i in range(1, m):
            next_st = st.copy()
            now_min_val = min(st)
            for j in range(max_range):
                min_val = now_min_val
                for x in c[i]:
                    last_x = j ^ x
                    min_val = min(min_val, st[last_x] - c[i][x])
                next_st[j] = d[i] + min_val
            st = next_st
        return st[0]
                

            




 

data = Solution()
# nums = [1,2,0,3,0]
# k = 1
# print(data.minChanges(nums, k))
# nums = [3,4,5,2,1,7,3,4,7]
# k = 3
# print(data.minChanges(nums, k))
# nums = [1,2,4,1,2,5,1,2,6]
# k = 3
# print(data.minChanges(nums, k))
nums = [26,19,19,28,13,14,6,25,28,19,0,15,25,11]
k = 3
print(data.minChanges(nums, k))